<?php
namespace app\shop\controller;

use think\Db;
use think\Session;
use think\Controller;

class Login extends Base
{
	public function _initialize(){
	}
	public function index()
	{   
		$isLogin=$this->isLogin();//halt($isLogin);
		if($isLogin){
			return $this->redirect('shop/index/index');
		}else{
			//如果后台用户已经登录了，那么我们需要跳转到后台页面
			return $this->fetch();
		}
	}

    //管理员登录
    public function check(){
		if(request()->isAjax())
        {
            $data=input('post.');//halt($data);
			$gname=$data['username'];
			$pwd=md5($data['password']);
			$rs=db('shop')->where(['username'=>$gname])->find();//halt($rs);
//            dump($rs);
            if ($rs==''){
                $rs = db('shop')->where(['phone'=>$gname])->find();//halt($rs);
            }
//            dump($rs);
            if($rs['status']==1) {
                if ($data['code'] == '') {
                    return array('code' => -1, 'msg' => "请输入验证码");
                }
                if (!captcha_check($data['code'])) {
                    return array('code' => -1, 'msg' => "验证码错误");
                }
                if ($rs) {
                    $cha = db('shop')->where(['username' => $gname, 'password' => $pwd])->whereOr('phone',$gname)->find();
                    if ($cha) {
                        Session::set('username', $gname);
                        Session::set('id', $cha['id']);
                        return array('code' => 1, 'msg' => "登录成功");
                    } else {
                        return array('code' => 0, 'msg' => "登录失败,账号或密码错误！");
                    }
                } else {
                    return array('code' => 0, 'msg' => "登录失败,账号或密码错误！");
                }
            }else{
                return array('code' => 2, 'msg' => "此账号已被锁定，请联系平台解封");
            }
        }
		else
		{
            return view('shop/login/index');
        }
	}

    public function welcome()
    {
        return view('welcome');
    }
	

    //退出登录代码
    public function tuichu()
    {
        Session(null);
        $this->redirect('shop/login/index');
    }
}
